import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Collection;
import java.util.Date;
import java.util.Set;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.ListModel;

import javax.swing.WindowConstants;

import chartRoom.ConstantUtil;
import chartRoom.Message;
import chartRoom.RoomUtil;

import javax.swing.SwingUtilities;

/**ddddddddddddddddd
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 * eclipse
 */
public class ClientJFrame extends javax.swing.JFrame implements ActionListener {
	private JLabel jLabel1;
	private String aaa;
	private JList jList1;
	private DefaultListModel<String> model;
	private JButton jButton1;
	private JButton jButton2;
	private JTextField jTextField1;
	private JComboBox comboBox;
	private String name;// 帐号
	private Socket s;// 和服务器连接的socket

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
			
			}
		});
	}

	public ClientJFrame(String name,Socket s) {
		super();
		initGUI();
		this.s=s;
		this.name=name;
		this.setTitle("欢迎你，"+name);
		//启动线程开始接受服务器的消息
		new ClientThread().start();
		//向服务器请求最新的在线用户列表
		
		try {
			//创建输出流
			OutputStream os=s.getOutputStream();
			ObjectOutputStream oos=new ObjectOutputStream(os);
			//将消息封装到对象中
			Message msg=new Message("sx",null, null,null);
			//发送消息
			oos.writeObject(msg);
			oos.flush();//清空缓存，强制发送消息
		   
		
		
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	private void initGUI() {
		try {
			
			this.addWindowListener(new WindowAdapter() {
			
				@Override
				public void windowClosing(WindowEvent e) {
					System.out.println("-------");
					
					Message msg=new Message("xiaxian",name,null,null);
					RoomUtil.sendMsg(s, msg);
					
					System.exit(0);
				}
			});
			
			
			
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("\u8bf7\u8f93\u5165\u6d88\u606f\u5185\u5bb9\uff1a");
				jLabel1.setBounds(51, 367, 133, 17);
			}
			{
				jTextField1 = new JTextField();
				getContentPane().add(jTextField1);
				jTextField1.setBounds(287, 362, 221, 28);
			}
			{
				jButton1 = new JButton();
				getContentPane().add(jButton1);
				jButton1.setText("\u53d1\u9001\u6d88\u606f");
				jButton1.setBounds(532, 360, 102, 28);
				// 注册监听
				jButton1.addActionListener(this);
			}
			{
				jButton2 = new JButton();
				getContentPane().add(jButton2);
				jButton2.setText("\u67e5\u770b\u5386\u53f2\u6d88\u606f");
				jButton2.setBounds(532, 395, 102, 22);
				jButton2.addActionListener(this);
				
			}
			{
				model=new DefaultListModel<String>();
				jList1 = new JList(model);
				getContentPane().add(jList1);

				jList1.setBounds(29, 35, 593, 276);
				
			}
			
			comboBox = new JComboBox();
			
			comboBox.setBounds(166, 365, 102, 21);
			getContentPane().add(comboBox);
			
			
			this.setSize(661, 453);
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

	/**
	 * 友情提示：将消息添加到面板上的代码是 model.addElement(消息);
	 * 			按钮禁用的代码是 jButton1.setEnabled(false);
	 */
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jButton2) {// 查看历史消息
			 JOptionPane.showMessageDialog(null,"[测试]这里显示读取历史消息文件的内容");
		
		}
		
		if (e.getSource() == jButton1) {// 发送消息
			// JOptionPane.showMessageDialog(null,"[测试] 向服务器发送消息");
			 //取值接受者账号
			String reciveName= comboBox.getSelectedItem().
					toString().trim();
			//取值聊天内容
			String content=jTextField1.getText().trim();
			if (content.equals("")) {
				JOptionPane.showMessageDialog(null,"请输入消息！");
			}else{
				//确定聊天方式  群聊  私聊
				Message msg=null;
				if (reciveName.equals("")) {
					msg=new Message("qunliao",this.name,
							null,content);	
				}else{
					msg=new Message("siliao",
							this.name,reciveName,content);
					//发送消息
					try {
						OutputStream os=s.getOutputStream();
						ObjectOutputStream oos=new ObjectOutputStream(os);
						//发送消息
						oos.writeObject(msg);
						oos.flush();//清空缓存，强制发送消息
					} catch (Exception e2) {
						e2.printStackTrace();
					}
					
				}
			}
		}
		
		
	}
	/**
	 * 关闭禁言的线程
	 * @author wangsong
	 *
	 */
	class Xxx extends Thread {
		@Override
		public void run() {
			try {
				sleep(5000);
				jButton1.setEnabled(true);			
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	// 负责处理接收服务器的消息
	class ClientThread extends Thread {
		@Override
		public void run() {

			//准备对象输入流 读取客户的消息
			try {
				InputStream in=s.getInputStream();
			    ObjectInputStream ois=null;
			    //客户不下线，服务员线程一直工作
			    while (true) {			    	
			    	if (in.available()>0) {//有消息就干活
			    		ois=new ObjectInputStream(in);
			    		Message msg=(Message)ois.readObject();//强转成消息对象
			    		if (msg.getOption().equals("tr")) {
							JOptionPane.showMessageDialog(null,"你已经被强制下线！");
							System.exit(0);
			    		}
			    		else if (msg.getOption().equals("jy")) {
							jButton1.setEnabled(false);
							new Xxx().start();
							
						}
			    		else if (msg.getOption().equals("siliao")) {
			    			//将服务器发送过来的消息放在聊天面板上
							model.addElement(msg.getContent());
						}
			    		else if (msg.getOption().equals("sx")) {
							comboBox.removeAllItems();
							String str=msg.getContent();
							String[] ss=str.split("-");
							for (String string : ss) {
								comboBox.addItem(string);
							}
							
							
						}else if (msg.getOption().equals("yj")) {//请求最新的在线列表
							jButton1.setEnabled(false);
						}
					}else{//否则休眠
						this.sleep(100);
					}
			    	
				}
			
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
			
			
			
			
			
			
		
		}
	}
	
	
	
	
	
	
}
